package com.stu.wgt.test.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.wgt.test.bean.Product;
import com.stu.wgt.test.bean.ProductPictrurl;
import com.stu.wgt.test.bean.ShoppingCart;
import com.stu.wgt.test.mapper.ProductMapper;
import com.stu.wgt.test.mapper.ProductPictrurlMapper;
import com.stu.wgt.test.mapper.ShoppingCartMapper;
import com.stu.wgt.test.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wgt
 * @since 2024-02-04
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductPictrurlMapper productPictrurlMapper;

    @Override
    public int insert(ShoppingCart shoppingCart) {
        return shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public int delete(int id) {
        return shoppingCartMapper.deleteById(id);
    }

    @Override
    public int update(ShoppingCart shoppingCart) {
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.eq("id", shoppingCart.getId());
        return shoppingCartMapper.update(shoppingCart, qw);
    }

    @Override
    public List<ShoppingCart> selectAllShoppingCart() {
        return shoppingCartMapper.selectList(null);
    }

    @Override
    public List<ShoppingCart> selectById(int id) {
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.eq("id", id);
        return shoppingCartMapper.selectList(qw);
    }

    @Override
    public List<ShoppingCart> selectByName(String username) {

        return null;
    }

    @Override
    public IPage<ShoppingCart> searchShoppingCart(Integer query, int pageNum, int pageSize) {
        Page<ShoppingCart> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.eq("user_id", query);
        IPage<ShoppingCart> shoppingCartIPage = shoppingCartMapper.selectPage(page, qw);
        for (ShoppingCart list : shoppingCartIPage.getRecords()) {
            // 查询商品名
            List<Product> productList = productMapper.selectList(new QueryWrapper<Product>()
                    .eq("id", list.getProductId()));
            list.setProductName(productList.get(0).getName());
            // 查询图片
            List<ProductPictrurl> productPictrurlList = productPictrurlMapper.selectList(new
                    QueryWrapper<ProductPictrurl>().eq("product_id", list.getProductId()));
            list.setProductUrl(productPictrurlList.get(0).getPictureUrl());
        }
        return shoppingCartIPage;
    }
}
